package com.springboot.admin.controller;

import com.springboot.admin.common.ResponseMap;
import com.springboot.admin.jwt.JwtAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("tools")
public class ToolsController {
    @GetMapping("/getCompressJs")
    public ResponseMap deploy() {

        // 获取当前目录
        ApplicationHome ah = new ApplicationHome(JwtAuthService.class);
        String docStorePath = ah.getSource().getParentFile().toString();
        String shpath = docStorePath + "/build.sh";//.sh文件的绝对路径
        log.info("shpath------------" + shpath);
        Process process = null;
        List<String> processList = new ArrayList<String>();
        try {
            // 修改权限，否则无效
            String chmod = "chmod 777 " + shpath;
            Process proc = Runtime.getRuntime().exec(chmod);
            process = Runtime.getRuntime().exec(shpath);
            process.waitFor();
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        for (String line : processList) {
            System.out.println(line);
        }
        return ResponseMap.success();
    }
}
